import { Component, Vue } from "vue-property-decorator";
@Component({})
export class cardMixin extends Vue {
  reader: any;
  created() {
    this.reader = ReaderFunction.createNew();
  }

  readCardId() {
    return new Promise((resolve, reject) => {
      const me = this as any;
      const reader = me.reader;
      reader.TyA_GetUID(0, (result) => {
        me.checkedResult(result, "读卡失败！")
          .then(() => {
            reader.Sys_SetBuzzer(reader.device_id, 20, null);
            resolve(result.data.snr);
          })
          .catch(() => {
            reject();
          });
      });
    });
  }
  // 检查返回消息
  checkedResult(result, errorMsg = "", hiddenMessage?) {
    return new Promise((resolve, reject) => {
      let message;
      if (result == null) {
        message = "接口调用失败，请检查服务器软件是否已开启！";
      } else if (result.code == 0) {
        message = result.msg;
      } else if (result.retval != 0) {
        if (result.func == "TyA_Request") {
          message = errorMsg + "请检查卡片是否存在，";
        } else if (result.func == "Sys_Open") {
          message = errorMsg + "请检查设备是否连接，";
        }
        message = message || errorMsg;
        message = message + "错误码：" + result.retval;
      }
      if (message) {
        !hiddenMessage &&
          this.$message({ message, type: "error" });
        reject({ message, code: result?.retval });
      } else {
        resolve({});
      }
    });
  }
}
